//
//  InviteRewardsView.swift
//  WalkNovel
//
//  Created by Kevin on 2023/12/20.
//

import UIKit

class InviteRewardsView: UIView {

    lazy var inviteImage: UIImageView = {
        let image = UIImageView(image: UIImage(named: "invite_reward_detail_bg"))
        return image
    }()
    
    lazy var bgView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        view.wn_cornerWithBorder(8)
        return view
    }()
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 13)
        label.textColor = .black
        label.textAlignment = .center
        label.text = "您的邀請碼"
        return label
    }()
    
    lazy var codeLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 32)
        label.textColor = .init(named: "red_FE6390")
        label.textAlignment = .center
        return label
    }()
    
    lazy var copyBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named: "invite_reward_copy"), for: .normal)
        btn.rx.tap.bind { [weak self] in
            WNShareManager.shared.copyUrlMethod(self?.codeLabel.text ?? "")
            WNHUD.wn_show("複製邀請碼成功")
        }.disposed(by: rx.disposeBag)
        return btn
    }()
    
    lazy var enterBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.setTitle("輸入邀請碼", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = .wn_font(name: .bold, size: 12)
        btn.setImage(UIImage(named: "library_rec_arrow_white"), for: .normal)
        btn.rx.tap.bind { [weak self] in
            self?.inviteClosure?("inviter")
        }.disposed(by: rx.disposeBag)
        btn.backgroundColor = WNColor.Fade().left2right(.init(width: 180, height: 36))
        btn.wn_cornerWithBorder(8)
        return btn
    }()
    
    lazy var inviteBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("現在邀請", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = .wn_font(name: .bold, size: 16)
        btn.rx.tap.bind { [weak self] in
            self?.inviteClosure?("invite")
        }.disposed(by: rx.disposeBag)
        btn.backgroundColor = WNColor.Fade().left2right(.init(width: scaleWidth(240), height: 44))
        btn.wn_cornerWithBorder(22)
        return btn
    }()

    var inviteClosure: StringClosure?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .clear
        addSubview(inviteImage)
        addSubview(bgView)
        bgView.addSubview(titleLabel)
        bgView.addSubview(codeLabel)
        bgView.addSubview(copyBtn)
        bgView.addSubview(enterBtn)
        bgView.addSubview(inviteBtn)
        inviteImage.snp.makeConstraints { make in
            make.top.left.right.equalTo(0)
            make.height.equalTo(scaleWidth(210))
        }
        bgView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-20)
            make.size.equalTo(CGSize.init(width: scaleWidth(335), height: 254))
        }
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(20)
        }
        codeLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(20)
            make.height.equalTo(45)
        }
        copyBtn.snp.makeConstraints { make in
            make.left.equalTo(codeLabel.snp.right).offset(6)
            make.centerY.equalTo(codeLabel.snp.centerY)
            make.width.height.equalTo(32)
        }
        enterBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(codeLabel.snp.bottom).offset(20)
            make.size.equalTo(CGSize(width: 180, height: 36))
        }
        inviteBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-20)
            make.width.equalTo(scaleWidth(240))
            make.height.equalTo(44)
        }
        enterBtn.layoutButton(style: .right, imageTitleSpace: 4)
        bgView.wn_cornerWithBorder(8, 0.1, .white, 1, .init(white: 0, alpha: 0.1), .zero, 8, 1)
        layer.masksToBounds = false
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class InviteTaskView: UIView {
    
    lazy var bgView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        view.wn_cornerWithBorder(8)
        return view
    }()
    
    lazy var leftLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 13)
        label.textColor = .init(named: "black_333333")
        label.textAlignment = .center
        label.text = "邀請成功"
        return label
    }()
    
    lazy var leftImage: UIImageView = {
        let image = UIImageView(image: UIImage(named: "invite_reward_add"))
        return image
    }()
    
    lazy var leftCount: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 24)
        label.textColor = .init(named: "red_FE6390")
        return label
    }()
    
    lazy var rightLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 13)
        label.textColor = .init(named: "black_333333")
        label.textAlignment = .center
        label.text = "我的獎勵"
        return label
    }()
    
    lazy var rightImage: UIImageView = {
        let image = UIImageView(image: UIImage(named: "reward_coin"))
        return image
    }()
    
    lazy var rightCount: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 24)
        label.textColor = .init(named: "red_FE6390")
        return label
    }()
    
    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = .init(named: "gray_D0D0D0")
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .clear
        addSubview(bgView)
        bgView.addSubview(leftLabel)
        bgView.addSubview(leftImage)
        bgView.addSubview(leftCount)
        bgView.addSubview(rightLabel)
        bgView.addSubview(rightImage)
        bgView.addSubview(rightCount)
        bgView.addSubview(lineView)
        bgView.snp.makeConstraints { make in
            make.top.equalTo(10)
            make.left.equalTo(20)
            make.bottom.right.equalTo(-20)
        }
        leftLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview().offset(-(ScreenW*0.25-10))
            make.top.equalTo(20)
        }
        leftImage.snp.makeConstraints { make in
            make.centerX.equalTo(leftLabel.snp.centerX).offset(-12)
            make.top.equalTo(leftLabel.snp.bottom).offset(20)
            make.height.width.equalTo(28)
        }
        leftCount.snp.makeConstraints { make in
            make.left.equalTo(leftImage.snp.right).offset(12)
            make.centerY.equalTo(leftImage.snp.centerY)
        }
        rightLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview().offset((ScreenW*0.25-10))
            make.top.equalTo(20)
        }
        rightImage.snp.makeConstraints { make in
            make.centerX.equalTo(rightLabel.snp.centerX).offset(-12)
            make.top.equalTo(rightLabel.snp.bottom).offset(20)
            make.height.width.equalTo(22)
        }
        rightCount.snp.makeConstraints { make in
            make.left.equalTo(rightImage.snp.right).offset(12)
            make.centerY.equalTo(rightImage.snp.centerY)
        }
        lineView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(30)
            make.bottom.equalTo(-30)
            make.width.equalTo(1)
        }
        bgView.wn_cornerWithBorder(8, 0.1, .white, 1, .init(white: 0, alpha: 0.1), .zero, 8, 1)
        layer.masksToBounds = false
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
